<template>
    <div class="app-container">
        <div class="filter-container" style="position: relative;">
            <el-form ref="listQuery" :model="listQuery" inline>
                <el-form-item label="签收时间" prop="filterTime" style="display: block;">
                    <el-date-picker size="small" clearable @change="setFilterTime" v-model="listQuery.filterTime" type="daterange" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" format="yyyy-MM-dd" value-format="yyyy-MM-dd">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="进厂批次" prop="batchCode">
                    <el-input size="small" clearable style="width: 150px;" class="filter-item" placeholder="请输入进厂批次" v-model="listQuery.batchCode">
                    </el-input>
                </el-form-item>
                <el-form-item label="物流单号" prop="logisCode">
                    <el-input size="small" clearable style="width: 150px;" class="filter-item" placeholder="请输入物流单号" v-model="listQuery.logisCode">
                    </el-input>
                </el-form-item>
                <el-form-item label="订单状态" prop="status">
                    <el-select size="small" class="filter-item" style="width: 130px" v-model="listQuery.status">
                        <el-option v-for="item in statusOptions" :key="item.id" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="签收人" prop="receiveName">
                    <el-input size="small" clearable style="width: 150px;" class="filter-item" placeholder="请输入签收人" v-model="listQuery.receiveName">
                    </el-input>
                </el-form-item>
                <el-form-item>
                    <el-button size="small" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
                    <el-button size="small" class="filter-item" type="danger" icon="el-icon" @click="resetFilter">重置</el-button>
                </el-form-item>
            </el-form>
            <div class="rightBtns">
                <el-button size="small" class="filter-item" icon="el-icon" @click="setSearchTime('1')">今天</el-button>
                <el-button size="small" class="filter-item" icon="el-icon" @click="setSearchTime('2')">昨天</el-button>
                <el-button size="small" class="filter-item" icon="el-icon" @click="setSearchTime('3')">近7天</el-button>
                <el-button size="small" class="filter-item" icon="el-icon" @click="setSearchTime('4')">本月</el-button>
            </div>
        </div>
        <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border fit highlight-current-row style="width: 100%" stripe>
            <el-table-column align="center" width="80px" type="index" label="序号"></el-table-column>
            <el-table-column align="center" label="进厂批次" prop="batchOrderCode"></el-table-column>
            <el-table-column align="center" label="物流单号" prop="code">
                <template slot-scope="scope">
                    {{scope.row.code}}
                </template>
            </el-table-column>
            <el-table-column align="center" label="状态" prop="statusName"></el-table-column>
            <el-table-column align="center" label="总袋数" prop="all_bagCnt"></el-table-column>
            <el-table-column align="center" label="装车袋数" prop="sent_bagCnt"></el-table-column>
            <el-table-column align="center" label="签收袋数" prop="received_bagCnt"></el-table-column>
            <el-table-column align="center" label="签收人" prop="receiveName"></el-table-column>
            <el-table-column align="center" label="签收时间">
                <template slot-scope="scope">
                    <span>{{scope.row.receiveTime}}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="操作" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button type="success" size="small" @click="handleShowDetail(scope.row)">查看明细</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination-container">
            <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page" :page-sizes="[20,50,100]" :page-size="listQuery.size" layout="total, sizes, prev, pager, next" :total="total">
            </el-pagination>
        </div>

        <el-dialog title="查看详情" center :visible.sync="dialogDetail" @close="dialogDetail=false">
            <detail :id="detailId"></detail>
        </el-dialog>
    </div>
</template>

<script>
  import { parseTime, formatSearchTime } from '@/utils'
  import { fetchLogisOrderList } from '@/api/logis'
  import detail from '@/views/receive/detail'

  export default {
    name: 'ReceiveList',
    data() {
      return {
        tableKey: 0,
        list: [],
        total: null,
        listLoading: false,
        listQuery: {
          page: 1,
          size: 20,
          batchCode: '',
          logisCode: '',
          status: 2,
          receiveName: '',
          startTime: '',
          endTime: '',
          filterTime: []
        },
        statusOptions: [{ name: '已签收', id: 2 }],
        dialogDetail: false,
        detailId: ''
      }
    },
    components: {
      detail
    },
    created: function() {
      this.getList()
    },
    filters: {
      parseTime
    },
    methods: {
      setSearchTime(t) {
        let st = ''
        let et = ''
        const now = new Date()
        switch (t) {
          case '1':
            st = parseTime(now, '{y}-{m}-{d}')
            et = st
            break
          case '2':
            st = parseTime((now.getTime() - 24 * 60 * 60 * 1000), '{y}-{m}-{d}')
            et = st
            break
          case '3':
            et = parseTime(now, '{y}-{m}-{d}')
            st = parseTime((now.getTime() - 7 * 24 * 60 * 60 * 1000), '{y}-{m}-{d}')
            break
          case '4':
            et = parseTime(now, '{y}-{m}-{d}')
            now.setDate(1)
            st = parseTime(now, '{y}-{m}-{d}')
            break
        }
        this.listQuery.filterTime = [st, et]
        this.setFilterTime()
      },
      getList() {
        this.listLoading = true
        var query = {
          type: 1,
          page: this.listQuery.page,
          size: this.listQuery.size,
          batchCode: this.listQuery.batchCode,
          logisCode: this.listQuery.logisCode,
          status: this.listQuery.status,
          receiveName: this.listQuery.receiveName,
          receiveStartTime: this.listQuery.startTime ? formatSearchTime(this.listQuery.startTime + ' ', true) : '',
          receiveEndTime: this.listQuery.endTime ? formatSearchTime(this.listQuery.endTime + ' ') : ''
        }
        fetchLogisOrderList(query).then(response => {
          this.list = response.data && response.data.list || []
          this.total = response.data.count || 0
          this.listLoading = false
        }).catch(e => {
          this.listLoading = false
        })
      },
      setFilterTime() {
        if (this.listQuery.filterTime && this.listQuery.filterTime.length) {
          this.listQuery.startTime = this.listQuery.filterTime[0]
          this.listQuery.endTime = this.listQuery.filterTime[1]
          return
        }
        this.listQuery.startTime = ''
        this.listQuery.endTime = ''
      },
      handleFilter() {
        this.listQuery.page = 1
        this.getList()
      },
      resetFilter() {
        this.$refs['listQuery'].resetFields()
        this.setFilterTime()
      },
      handleShowDetail(row) {
        this.detailId = row.id
        this.dialogDetail = true
      },
      handleSizeChange(val) {
        this.listQuery.size = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.getList()
      }
    }
  }
</script>
<style scoped>
    .rightBtns {
        float: right;
        position: absolute;
        top: 1px;
        right: 10px;
    }
    .el-range-editor.el-input__inner {
        width: 273px;
        padding: 3px;
    }
</style>
